package com.example.restatr.Atcivity.Main;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentTransaction;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.Fragment.PartyBuildFragment;
import com.example.restatr.Atcivity.Main.Fragment.UserFragment;
import com.example.restatr.Atcivity.Main.Fragment.HomeFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private BottomNavigationView BNV;
    public static SharedPreferences sp;
    public static SharedPreferences.Editor edit;
    private Toast toast;
    public static String info = null;
    private HomeFragment homeFragment;
    private PartyBuildFragment partyBuildFragment;
    private UserFragment userFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        BNV = findViewById( R.id.BNV );
        barTitle.setVisibility( View.GONE );
        search.setVisibility( View.VISIBLE );
        sp = getSharedPreferences( "userinfo", MODE_PRIVATE );
        edit = sp.edit();
        search.setOnEditorActionListener( new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                int i = HomeFragment.tabTitle.indexOf( v.getText().toString() );
                if (i == -1) {
                    Toast.makeText( MainActivity.this, "暂无该新闻条目!~", Toast.LENGTH_SHORT ).show();
                } else {
                    HomeFragment.XwPager.setCurrentItem( i );
                    Toast.makeText( MainActivity.this, "已为您跳转到该新闻条目~", Toast.LENGTH_SHORT ).show();
                }
                return true;
            }
        } );
        if (savedInstanceState==null){
            homeFragment=new HomeFragment();
            getSupportFragmentManager().beginTransaction().add( R.id.frame,homeFragment ).commit();
        }
        BNV.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                hideFragment(transaction);
                switch (menuItem.getItemId()) {
                    case R.id.home:
                        barTitle.setVisibility( View.GONE );
                        search.setVisibility( View.VISIBLE );
                        if (homeFragment==null){
                            homeFragment=new HomeFragment();
                            transaction.add( R.id.frame,homeFragment );
                        }else {
                            transaction.show( homeFragment );
                        }
                        break;
                    case R.id.Dj:
                        barTitle.setVisibility( View.VISIBLE );
                        barTitle.setText( "智慧党建" );
                        search.setVisibility( View.GONE );
                        if (partyBuildFragment ==null){
                            partyBuildFragment =new PartyBuildFragment();
                            transaction.add( R.id.frame, partyBuildFragment);
                        }else {
                            transaction.show(partyBuildFragment);
                        }
                        break;
//                    case R.id.SJFX:
//                        barTitle.setVisibility(View.VISIBLE);
//                        barTitle.setText("数据分析");
//                        search.setVisibility(View.GONE);
//                        fragment = new WmSjFragment();
//                        break;
                    case R.id.GeRen:
                        barTitle.setVisibility( View.VISIBLE );
                        barTitle.setText( "个人中心" );
                        search.setVisibility( View.GONE );
                        if (userFragment ==null){
                            userFragment =new UserFragment();
                            transaction.add( R.id.frame, userFragment);
                        }else {
                            transaction.show(userFragment);
                        }
                        break;
                }
                transaction.commitAllowingStateLoss();
                return true;
            }
        } );
        tb.getMenu().clear();
        GetToken();
    }

    /**
     * 获取token,防止token过期
     */
    private void GetToken() {
        if (sp.getInt( "code", 0 ) == 200) {
            new Thread( new Runnable() {
                @Override
                public void run() {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put( "username", sp.getString( "username", null ) );
                        jsonObject.put( "password", sp.getString( "password", null ) );
                        String post = Tools.Post( "/prod-api/api/login", "", jsonObject.toString() );
                        if (post != null) {
                            JSONObject jiexi = new JSONObject( post );
                            if (jiexi.getInt( "code" ) == 200) {
                                edit.putString( "token", jiexi.getString( "token" ) ).commit();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            } ).start();
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        if (homeFragment!=null){
            transaction.hide( homeFragment );
        }
        if (partyBuildFragment !=null){
            transaction.hide(partyBuildFragment);
        }
        if (userFragment !=null){
            transaction.hide(userFragment);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("TAG", "onResume: "+info);
        info=null;
    }
}